package com.examples.controls.concepts;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class ResizeEvent {

	/**
	 * @param args
	 */
	static final int INSET = 10;
	public static void main(String[] args) {
	    Display display = Display.getDefault();
	    final Shell shell = new Shell(display);
	    final Button b1 = new Button(shell, SWT.PUSH);
	    final Button b2 = new Button(shell, SWT.PUSH);
	    
	    shell.addListener(SWT.Resize, new Listener() {
	        public void handleEvent(Event event) {
	        	
	            Rectangle rect = shell.getClientArea();
	            
	            /* Removing the side spaces and the space between the two buttons giving us the width of a button after being resized */
	            int width = (rect.width - INSET * 3) / 2;
	            
	            /* Setting x,y as 10 with (0,0) being the Coordinate axis of the shell */ 
	             /* Setting the width of button as width variable value  */
	            b1.setBounds(INSET,INSET,width,rect.height - INSET * 2);
	            
	            /* Setting x as the spaces between the buttons and the initial space and the width of first button
	             * which gives us width = width + INSET + INSET 
	             * and y as 10 with (0,0) being the Coordinate axis of the shell */ 
	             /* Setting the width of button as width variable value   */
	            b2.setBounds(width + INSET * 2,INSET,width,rect.height - INSET * 2);
	        }
	    });
	    shell.setSize(200, 200);
	    shell.open();
	    while (!shell.isDisposed()) {
	    	 if (!display.readAndDispatch()) display.sleep();
	    }
	  //display.dispose(); //Commented only for SWTShowcase application.
	}
}
